var dialogObject=null;
 function jGrid(){
$("input[type='text'],input[type='password']").addClass("text ui-widget-content ui-corner-all");
$("div[class='form']").hide();
$("table[class='grid'] thead").addClass("ui-widget-header");
$("table[class='grid'] tbody ").addClass("ui-widget-content");
$("table[class='grid'] tbody tr").css("border","1px solid transparent");
$("table[class='grid'] tbody tr td").css("font-weight","small");
$("table[class='grid'] tbody tr:first").attr("active","Y");
$("table[class='grid'] tbody tr:first td:first").addClass("ui-state-default");
var sortColumn=new Array();
$("div[class='grid_holder'] a").before('<div class="ui-toolbar ui-widget-header ui-helper-clearfix" style=" padding:5px;padding-right:20px; "> <span style="position: absolute;" class="btnSearch ui-state-default" title="New" onClick="openDialog()" ><span class="ui-icon ui-icon-circle-plus"></span></span>  <div style="float:right"><input type="text" onkeydown="keySearchGridData(event);" id="jGridSearchBox" placeholder="Search" class="text ui-widget-content ui-corner-all"><span style="position: absolute;" class="btnSearch ui-state-default" title="search"><span class="ui-icon ui-icon-search" onClick="searchGridData();"></span></span>   </div> </div> ');
$("table[class='grid'] tbody tr").hover(function(){
													  
													    	$("table[class='grid'] tbody tr[active='N'] td:first").removeAttr('class');
															 
														   $(this).find("td:first").addClass("ui-state-default");
													   },function(){
														    $(this).find("td:first").removeAttr("class");
															$("table[class='grid'] tbody tr[active='N'] td:first").removeAttr('class');
														   $("table[class='grid'] tbody tr[active='Y'] td:first").addClass('ui-state-default');

														   });
														    $('table[class="grid"] tbody tr').click(function(){
													 
															   $("table[class='grid'] tbody tr td:first").removeAttr('class');
															    $("table[class='grid'] tbody tr").attr('active','N');
															$(this).attr("active","Y");
														   $(this).find("td:first").addClass("ui-state-default");
															});
														
									 $('table[class="grid"] tbody tr td span[class="ui-icon ui-icon-pencil"]').click(function(){
 
															$(this).parent().parent().click();													   $(this).find("td:first").addClass("ui-state-default");

									 keyMouseListener(32);

									 }  );
									 }

 


$(document).keydown(function(e){
var keyCode=e.which;$(document).keydown(function(e){
var keyCode=e.which;
 
 if($("a[navigate]").is(":focus")){
 keyMouseListener(keyCode);
 return false;
 }

});
});

function popupcreate(){
	 
	try{
			var idpop=document.getElementById("erpPopUp");
			document.body.removeChild(idpop);
			}catch(e){
			
			}
			var newEle=	document.createElement("div");
			newEle.id="erpPopUp";
			document.body.appendChild(newEle);
	dialogObject=$("div[id='erpPopUp']").dialog({
			autoOpen: false,
			height: 'auto',
			width: 'auto',
			modal: true,
			 
			close:function(){
		
		 closeDialog();
			}
			});
	$(dialogObject).html($("div[class='form']").html());
	}
 
  
function openDialog(state){
	popupcreate();
	if(typeof state === 'undefined'){
		$("input[type='text']").val("");
		$("select :nth-child(0)").prop('selected', true);
		 $("table[class='grid'] tbody tr:first td[edit]").each(function(){
			
			 if( $(this).attr("edit")=="false"){
				 var name=$(this).attr("field");
				 var key='div[id="erpPopUp"] [name="'+name+'"]';
				 $(key).prop("disabled",false);
			}
		 });
	}
$(dialogObject).dialog("open");

} 
function closeDialog(){
	 
 
 
try{
		//	var idpop=document.getElementById("erpPopUp");
		 
		$(dialogObject).remove();
	 
			 
			dialogObject=null;
			}catch(e){
			 
			}
}

function edit(){
openDialog('E');
	 $("table[class='grid'] tbody tr[active='Y'] td[field]").each(function(){
		 var name=$(this).attr("field");
		 var key='[name="'+name+'"]';
		 var val=$(this).text().trim();
		 if($(key).is(":checkbox")){
		 if(val=='Y'){
		 $(key).prop("checked",true);
		 
		 }else{
		$(key).prop("checked",false);

		 
		 }
		 }else{
		  $(key).val(val);
		 
		 }
		  //alert($(this).attr("edit"));
		 if($(this).attr("edit")=="false"){
		 $(key).attr("disabled","disabled");
		 }
		 });
		 

		 
 



}

function keyMouseListener(keyCode){
$(document).find("table[class='grid'] tbody tr td:first").removeAttr("class");
 var activeRowTd=$(document).find("table tbody tr[active='Y']");
 var trNext;
 if($(activeRowTd).length>0){
 
 if(keyCode==40){
 trNext=$(activeRowTd).next();
  
 }
 if(keyCode==38){
 trNext =$(activeRowTd).prev();
  
 }
 
  }else{
 activeRowTd=$(this).find("table[class='grid'] tbody tr:first");
 trNext=$(this).find("table[class='grid'] tbody tr:first");
 
 }
 if($(trNext).index()==-1){
   trNext =$(document).find("table tbody tr[active='Y']");
   
 }
 $(activeRowTd).attr('active',"N");
 $(activeRowTd).find("td:first").removeAttr('class');
 $(trNext).attr("active","Y");
 $("table[class='grid'] tbody tr[active='Y']  td:first").addClass('ui-state-default');
 //alert($(trNext).index());
 if($(trNext).index()!=-1){
 

  
 var callbacks=$.Callbacks();
  var gridFocus=$("table[class='grid']");
  $("input[type='text']").val();
 var canDelete=false,canEdit=false;
 if($(gridFocus).attr("onDelete")){
  
 canDelete=true;
 } 
 if($(gridFocus).attr("onEdit")){
  
 canEdit=true;
 } 
 if(keyCode==46){
 //Delete
 if(canDelete){
 $(trNext).find('span[class="ui-icon ui-icon-trash"]').click();
 }else{
 alert('Delete operation not implemented')
 }
 }
 if(keyCode==32){
 //Edit
 if(canEdit){
  edit();
 }else{
 alert('Edit operation not implemented')
 }
 }
 
 }
  return false;

}

function keySearchGridData(e){
	var keyCode=e.which;
	if(keyCode==13){
		searchGridData();
		}
	
}
function searchGridData(){
	 var gridFocus=$("table[class='grid']");
	 
 
	 
	 		 $.post( $(gridFocus).attr("searchAction"), {query:$("#jGridSearchBox").val()} ).done(function(data){
			 
			 $(dialogObject).dialog('destroy').remove();
																$("div[id='loadData']").html(data);
																	
																													});
	  
	
}
$.ctrl = function(key, callback, args) {
	var isCtrl = false;
	$(document).keydown(function(e) {
	if(!args) args=[]; // IE barks when args is null

	if(e.ctrlKey) isCtrl = true;
	var kc = parseInt(key);
	if((!isNaN(kc) && kc > 0 && e.keyCode == key || isNaN(kc) && e.keyCode == key.charCodeAt(0)) && isCtrl) {
	callback.apply(this, args);
	return false;
	}
	}).keyup(function(e) {
	if(e.ctrlKey) isCtrl = false;
	});
	};

	$.ctrl(45, function() {
						try{
						openDialog();
						}catch(e){
							
							}
						return false; });